﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using JinianNet.JNCms.Filters;
using JinianNet.JNCms.Models;

namespace JinianNet.JNCms.Controllers
{
    [ManagerFilter()]
    public class ArticleController : ControllerBase
    {
        //public ActionResult Content(string key)
        //{
        //    return View();
        //}
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(string item, int page = 1, int size = 0)
        {
            if (size <= 0)
                size = Config.Page.Size;
            Category model = Data.DataAccess.CreateCategory().GetItem(item);
            if (model == null)
                return PageNotFound();
            //ViewBag.Category = model;
            ViewData["Category"] = model;
            var list = Data.DataAccess.CreateArticle().GetList(model.ID, page, size);

            string home, pre, next;
            if (page > 1)
            {
                home = Url.Action("List", "Article");
                pre = Url.Action("List", "Article", new { item = item, page = page - 1 });

            }
            else
            {
                home = null;
                pre = null;
            }
            if (list.Count == size)
            {
                next = Url.Action("List", "Article", new { item = item, page = page + 1 });
            }
            else
            {;
                next = null;
            }
            ViewData["WebPage"] = JinianNet.Web.PageLinker.GetActionLinker(home, pre, next);
            return View(list);
        }

        //[Authorize(Users="")]
        public ActionResult Edit(string item)
        {
            Article model = Data.DataAccess.CreateArticle().GetItem(item);
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)] 
        //[Authorize(Users = "")]
        public ActionResult Edit(Article model)
        {
            model.EditTime = DateTime.Now;
            if (Data.DataAccess.CreateArticle().Update(model) > 0)
            {
                Category category = Data.DataAccess.CreateCategory().GetItem(model.ChannelID);
                if (category != null)
                {
                    return Content(Config.GetTips("恭喜，编辑成功", Url.Action("List", "Article", new { item = category.KeyName }), true));
                }
                else
                {
                    return Content(Config.GetTips("恭喜，编辑成功", Url.Action("List", "Article"), true));
                }
            }
            else
            {
                return Content(Config.GetTips("编辑失败，请重试", Url.Action("Edit", "Article", new { model = model }), false));
            }
        }

        [HttpPost]
        [ValidateInput(false)] 
        //[Authorize(Users = "")]
        public ActionResult Insert(Article model)
        {
            if (string.IsNullOrEmpty(model.KeyName))
            {
                model.KeyName = string.Concat(JinianNet.Common.Random.RandAlphabet(1), JinianNet.Common.Random.RandNumber(9));
            }
            else
            {
                if (!JinianNet.Common.Utils.IsNumberOrLetter(model.KeyName.Replace("-", "").Replace("_", ""), true))
                {
                    return Content(Config.GetTips("对不起，文章别名只能为字母，数字与“-”或“_”的组合，且只能以字母开头！",null, false));
                }
            }
            model.CreateTime = DateTime.Now;
            model.EditTime = DateTime.Now;
            Category category = Data.DataAccess.CreateCategory().GetItem(model.ChannelID);
            if (category == null)
            {
                return Content(Config.GetTips("对不起，类别不存在", null, false));
            }
            else
            {
                model.ChannelName = category.Title;
                Data.DataAccess.CreateArticle().Insert(model);
                return Content(Config.GetTips("操作成功", Url.Action("Insert", "Article", new { item = category.KeyName }), true));
            }
            
        }

        //[Authorize(Users = "")]
        public ActionResult Insert(string item)
        {
            Article model = new Article();
            Category category = Data.DataAccess.CreateCategory().GetItem(item);
            if (category == null)
                return PageNotFound();
            model.ChannelID = category.ID;
            model.ChannelName = category.Title;
            model.TempLate = category.ChildTempLate;
            model.CreateTime = DateTime.Now;
            model.EditTime = DateTime.Now;
            model.KeyName = string.Concat(JinianNet.Common.Random.RandAlphabet(1), JinianNet.Common.Random.RandTime(3));
            //object[] values = JinianNet.Reflection.DynamicAccess.GetCustomAttributes<ManagerFilter>(this.GetType(), false);
            //object[] values = System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(ManagerFilter), true);
            //for(int i=0;i<values.Length;i++){
            //    ManagerFilter attr = values[i] as ManagerFilter;
            //    if(attr!=null){
            //        model.Editor = attr.User.Account;
            //        break;
            //    }
            //}
            
            return View("Edit", model);
        }

        public ActionResult Delete(string item)
        {
            Data.IArticle dal = Data.DataAccess.CreateArticle();
            if (item.IndexOf(',') < 0)
                dal.Delete(int.Parse(item));
            else
                foreach (string value in item.Split(','))
                    if (!string.IsNullOrEmpty(value))
                        dal.Delete(int.Parse(value));
            return Content(Config.GetTips("操作成功",null, true));
        }
    }
}
